package cluster.traversal;

import text.Text;
import text.TextSetManager;
import text.traversal.ITextTraversal;

public class GetBestDistance implements ITextTraversal
{
	private final TextSetManager textSetManager;
	
	private final String[] wordTab;
	private final double[] wordDistance;
	
	public String[] getBestDistance()
	{
		return this.wordTab;
	}
	
	public GetBestDistance(TextSetManager textSetManager, int bestWordCount)
	{
		this.textSetManager = textSetManager;
		wordDistance = new double[bestWordCount];
		wordTab = new String[bestWordCount];
		for(int i = 0; i < bestWordCount; i++)
		{
			wordDistance[i] = Double.POSITIVE_INFINITY;
		}
	}
	
	public void traversal(String word, Text text)
	{				
		double distance = 0;
		for(Text text2 : this.textSetManager.getTextList())
		{
			Double value = text2.getInfo(ComputeTFIDF.TFIDF, word);
			double tfidf1 = value!=null?value:0;
			
			String textTFIDFMean = ComputeMean.getInfoName(text2.getName(), ComputeTFIDF.TFIDF);
			value = text.getInfo(textTFIDFMean);
			double tfidf2 = value!=null?value:0;

			distance += tfidf1 * tfidf2;
		}
		
		distance = 1 - distance;
		
		int bestIndex = -1;
		double bestDistance = distance;
		for(int i = 0; i < wordDistance.length; i++)
		{
			if(bestDistance <= wordDistance[i])
			{
				bestDistance = wordDistance[i];
				bestIndex = i;
			}
		}
		
		if(bestIndex >= 0)
		{
			wordDistance[bestIndex] = distance;
			wordTab[bestIndex] = word;
		}
	}
}
